package com.changgou.web.gateway.filter;

import com.changgou.web.gateway.serivce.AuthService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

@Component
public class AuthFilter implements GlobalFilter, Ordered {

    public static final String Authorization = "Authorization";

    //登录地址
    public static final String LOGIN_URL = "http://localhost:8001/api/oauth/toLogin";

    @Autowired
    private AuthService authService;

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        //获取当前请求对象
        ServerHttpRequest request = exchange.getRequest();
        ServerHttpResponse response = exchange.getResponse();

        String path = request.getURI().getPath();
        if ("/api/oauth/login".equals(path) || !UrlFilter.hasAuthorize(path)) {
            //放行
            return chain.filter(exchange);
        }

        //判断cookie上是否存在jti也就是短令牌
        String jti = authService.getJtiFromCookie(request);
        if (StringUtils.isEmpty(jti)) {
            //拒绝访问,请求跳转
            //            response.setStatusCode(HttpStatus.UNAUTHORIZED);
            //            return response.setComplete();
            return this.toLoginPage(LOGIN_URL + "?FROM=" + request.getURI(), exchange);
        }

        //根据短令牌获取长令牌, 并且判断是否有长令牌
        String token = authService.getJwtFromRedis(jti);
        if (StringUtils.isEmpty(token)) {
            //拒绝访问，请求跳转
            //            response.setStatusCode(HttpStatus.UNAUTHORIZED);
            //            return response.setComplete();
            return this.toLoginPage(LOGIN_URL + "?FROM=" + request.getURI(), exchange);
        }

        //校验通过 , 请求头增强，放行(将长令牌放入请求头)
        request.mutate().header(Authorization, "Bearer " + token);
        return chain.filter(exchange);
    }

    /***
     * 跳转到登录页面
     * @param loginUrl  跳转的地址
     * @return
     */
    private Mono<Void> toLoginPage(String loginUrl, ServerWebExchange exchange) {
        ServerHttpResponse response = exchange.getResponse();
        response.setStatusCode(HttpStatus.SEE_OTHER);
        response.getHeaders().set("Location", loginUrl);
        return response.setComplete();
    }

    @Override
    public int getOrder() {
        return 0;
    }
}